<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


class ZOrder {
	static public function OnlineIt($order_id, $pay_id, $money, $currency='CNY', $service='alipay', $bank='支付宝',$trade_no=''){ 
		list($_, $_, $quantity, $_) = explode('-', $pay_id);
		
		if (!$order_id || !$pay_id ) return false;
		
		$condition = array( 
			 "packageid = $order_id or id = $order_id",
			 'state'=>'unpay',
			);
		$order_list = DB::LimitQuery('order', array(
			'condition' => $condition,
		));
		
		$order = Table::Fetch('order', $order_id);
		$money=moneyit($order['origin']-$order['credit']-$order['point']);
		
		$user_id = abs(intval($order['user_id']));
		
		
		//购买充值
		if ( $order['state'] == 'unpay' ) {

			$table = new Table('pay');
			$table->id = $pay_id;
			$table->vid = $trade_no;
			$table->order_id = $order_id;
			$table->money = $money;
			$table->currency = $currency;
			$table->bank = $bank;
			$table->service = $service;
			$table->create_time = time();
			$ia = array('id','vid', 'order_id', 'money', 'currency', 'service', 'create_time', 'bank');
			if (Table::Fetch('pay', $pay_id) || ! $table->insert($ia)) {
			   	return false;
			}
			//update user money; +money
			Table::UpdateCache('user', $user_id, array(
						'money' => array( "money + {$money}" ),
						));             
			$u = array(
					'user_id' => $user_id,
					'admin_id' => 0,
					'money' => $money,
					'direction' => 'income',
					'action' => 'paycharge',
					'detail_id' => $pay_id,
					'create_time' => time(),
				  );
			DB::Insert('flow', $u);
			$user = Table::FetchForce('user', $user_id);
            //print_r($user);exit;
		}
		foreach($order_list as $k=>$value){
			$team = Table::Fetch('team', $value['team_id']);
			team_state($team);
			 
			/*if ($user['money']<$value['origin']){
				return false;
			}*/

			if (in_array($team['state'], array('soldout')) ||  $team['end_time']<time()) {
				return false;
			}
			$pay_money=$value['money'];
			if($pay_money>0){
				Table::UpdateCache('order', $value['id'], array(
							'pay_id' => $pay_id,
							'money' => $pay_money,
							'state' => 'pay',
							'trade_no' => $trade_no,
							'service' => $service,
							'pay_time' => time(),
							));
			}else{
				Table::UpdateCache('order', $value['id'], array(
							'money' => 0,
							'state' => 'pay',
							'service' =>'credit',
							'pay_time' => time(),
							));
			}
			$ord = Table::FetchForce('order', $value['id']);
			if ( $ord['state'] == 'pay' ) {
				//TeamBuy Operation
				ZTeam::BuyOne($ord);
			}
		}
		return true;
	}

	static public function CashIt($order) {
		global $login_user_id;
		if (! $order['state']=='pay' ) return 0;

		//update order
		Table::UpdateCache('order', $order['id'], array(
					'state' => 'pay',
					'service' => 'cash',
					'admin_id' => $login_user_id,
					'money' => $order['origin'],
					'pay_time' => time(),
					));
		  /* cash flow */
		$order = Table::FetchForce('order', $order['id']);
                ZFlow::CreateFromStore($order['user_id'], $order['origin']);
		ZTeam::BuyOne($order);
	}

	static public function CreateFromCharge($money, $user_id, $time,$service) {
		if (!$money || !$user_id || !$time || !$service) return 0;
		$pay_id = "charge-{$user_id}-{$time}";
		$oarray = array(
				'user_id' => $user_id,
				'pay_id' => $pay_id,
				'service' => $service,
				'state' => 'pay',
				'money' => $money,
				'origin' => $money,
				'create_time' => $time,
			       );
		return DB::Insert('order', $oarray);
	}
}
?>
